/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.dualaccgen.method;


import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.entity.entity.ICefData;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.AccessorClassGenContext;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.AccessorUtil;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.cef.generator.repository.RepositoryGenUtil;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.ThrowStatement;

public class DualAcsCreateChildGenerator extends ClassMethodGenerator {

  private final IGspCommonDataType node;
  private HashMap<String, String> childTypes;
  private static String varChildCode = "childCode";

  public DualAcsCreateChildGenerator(IGspCommonDataType node, HashMap<String, String> childTypes,
      AccessorClassGenContext rootContext) {
    this.node = node;
    this.childTypes = childTypes;
  }

  @Override
  protected String getMethodName() {
    return "innerCreateChild";
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo(ICefData.class.getTypeName());
  }

  @Override
  protected ArrayList<ModifierKeyword> getAccessModifiers() {
    ArrayList<ModifierKeyword> list = new ArrayList<>();
    list.add(ModifierKeyword.PROTECTED_KEYWORD);
    return list;
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    ArrayList<ParameterInfo> list = new ArrayList<>();
    ParameterInfo parameterInfo = new ParameterInfo();
    parameterInfo.setParamName("childCode");
    parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
    list.add(parameterInfo);
    return list;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    addCheckReadonly(block);
    addBlockIfStatements(block);
    addThrowNotSupportChildCodeException(block);
    return block;
  }

	private void addCheckReadonly(Block block) {
		SuperMethodInvocation methodInvocation = ast.newSuperMethodInvocation();
		methodInvocation.setName(ast.newSimpleName("checkReadonly"));
		block.statements().add(ast.newExpressionStatement(methodInvocation));
	}

	private void addBlockIfStatements(Block block) {
    for (Map.Entry<String, String> item : childTypes.entrySet()) {
      IfStatement ifStatement = ast.newIfStatement();
      addIfExpression(ifStatement, item);
      addIfThenStatement(ifStatement, item);
      block.statements().add(ifStatement);
    }
  }

  private void addIfExpression(IfStatement ifStatement, Map.Entry<String, String> item) {
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setExpression(ast.newSimpleName(varChildCode));
    methodInvocation.setName(ast.newSimpleName("equals"));
    methodInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast, item.getKey()));
    ifStatement.setExpression(methodInvocation);
  }

  private void addIfThenStatement(IfStatement ifStatement, Map.Entry<String, String> item) {
    Block block = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(ast.newSimpleType(ast.newName(item.getValue())));

    addNewAccessorArgs(item, classInstanceCreation);
    returnStatement.setExpression(classInstanceCreation);
    block.statements().add(returnStatement);
    ifStatement.setThenStatement(block);
  }

  private void addNewAccessorArgs(Map.Entry<String, String> item,
      ClassInstanceCreation classInstanceCreation) {
    CastExpression castExpression = ast.newCastExpression();
    String childDataType = ((IGspCommonObject) node).getBelongModel()
        .findObjectByCode(item.getKey()).getGeneratedEntityClassInfo().getClassFullName();
    castExpression.setType(ast.newSimpleType(ast.newName(childDataType)));
    addCastExpression(item, castExpression);
    classInstanceCreation.arguments().add(castExpression);

    classInstanceCreation.arguments().add(RepositoryGenUtil.createMethodInvocation(ast, null,
        AccessorUtil.MethodName_AccIsReadonly, null));
  }

  private void addCastExpression(Map.Entry<String, String> item, CastExpression castExpression) {
    MethodInvocation methodInvocation = ast.newMethodInvocation();

    methodInvocation.setName(ast.newSimpleName("createChild"));
    addInnerCreateChildExpression(methodInvocation, item);
    methodInvocation.arguments().add(ast.newSimpleName(varChildCode));
    castExpression.setExpression(methodInvocation);
  }

  private void addInnerCreateChildExpression(MethodInvocation methodInvocation,
      Map.Entry<String, String> item) {
    MethodInvocation innerInvocation = ast.newMethodInvocation();
    innerInvocation.setName(ast.newSimpleName("getInnerData"));
    methodInvocation.setExpression(innerInvocation);
  }

  private void addThrowNotSupportChildCodeException(Block block) {
//  SetExceptionCode setExceptionCode = new SetExceptionCode();
    ThrowStatement throwStatement = GeneratorUtil.getThrowStatement(ast,"不存在子表",varChildCode,true);
    addImport(CefException.class.getTypeName());
//    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
//    setExceptionCode.setClassInstanceCreation(classInstanceCreation,ast,"不存在子表",varChildCode);
//    classInstanceCreation.setType(new TypeInfo(RuntimeException.class).getType(ast));
//    InfixExpression infixExpression = ast.newInfixExpression();
//    infixExpression.setOperator(InfixExpression.Operator.PLUS);
//    StringLiteral stringLiteral = ast.newStringLiteral();
//    stringLiteral.setLiteralValue("不存在子表");
//    infixExpression.setLeftOperand(stringLiteral);
//    infixExpression.setRightOperand(ast.newSimpleName(varChildCode));
//    classInstanceCreation.arguments().add(infixExpression);
    //throwStatement.setExpression(classInstanceCreation);
    block.statements().add(throwStatement);
  }
}
